home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / termcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  14.4 KB  |  708 lines

  1. /* Work-alike for termcap, plus extra features.
  2.    Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: Not synched with FSF. */
  21.  
  22. /* config.h may rename various library functions such as malloc.  */
  23. #ifdef emacs
  24. #include <config.h>
  25. #include "lisp.h" /* For encapsulated open, close, read */
  26. #include "device.h" /* For DEVICE_BAUD_RATE */
  27. #else /* not emacs */
  28. #if defined(USG) || defined(STDC_HEADERS)
  29. #define memcpy(d, s, n) memcpy ((d), (s), (n))
  30. #endif
  31.  
  32. #ifdef STDC_HEADERS
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #else
  36. char *getenv ();
  37. char *malloc ();
  38. char *realloc ();
  39. #endif
  40.  
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44. #ifdef _POSIX_VERSION
  45. #include <fcntl.h>
  46. #endif
  47.  
  48. #endif /* not emacs */
  49.  
  50. /* BUFSIZE is the initial size allocated for the buffer
  51.    for reading the termcap file.
  52.    It is not a limit.
  53.    Make it large normally for speed.
  54.    Make it variable when debugging, so can exercise
  55.    increasing the space dynamically.  */
  56.  
  57. #ifndef BUFSIZE
  58. #ifdef DEBUG
  59. #define BUFSIZE bufsize
  60.  
  61. int bufsize = 128;
  62. #else
  63. #define BUFSIZE 2048
  64. #endif
  65. #endif
  66.  
  67. #ifndef emacs
  68. static void
  69. memory_out ()
  70. {
  71.   write (2, "virtual memory exhausted\n", 25);
  72.   exit (1);
  73. }
  74.  
  75. static char *
  76. xmalloc (size)
  77.      unsigned int size;
  78. {
  79.   char *tem = malloc (size);
  80.  
  81.   if (!tem)
  82.     memory_out ();
  83.   return tem;
  84. }
  85.  
  86. static char *
  87. xrealloc (ptr, size)
  88.      char *ptr;
  89.      unsigned size;
  90. {
  91.   char *tem = realloc (ptr, size);
  92.  
  93.   if (!tem)
  94.     memory_out ();
  95.   return tem;
  96. }
  97. #endif /* not emacs */
  98.  
  99. /* Looking up capabilities in the entry already found.  */
  100.  
  101. /* The pointer to the data made by tgetent is left here
  102.    for tgetnum, tgetflag and tgetstr to find.  */
  103. static char *term_entry;
  104.  
  105. static CONST char *tgetst1 (CONST char *ptr, char **area);
  106.  
  107. /* Search entry BP for capability CAP.
  108.    Return a pointer to the capability (in BP) if found,
  109.    0 if not found.  */
  110.  
  111. static CONST char *
  112. find_capability (bp, cap)
  113.      CONST char *bp;
  114.      CONST char *cap;
  115. {
  116.   for (; *bp; bp++)
  117.     if (bp[0] == ':'
  118.     && bp[1] == cap[0]
  119.     && bp[2] == cap[1])
  120.       return &bp[4];
  121.   return 0;
  122. }
  123.  
  124. int
  125. tgetnum (cap)
  126.      CONST char *cap;
  127. {
  128.   CONST char *ptr = find_capability (term_entry, cap);
  129.   if (!ptr || ptr[-1] != '#')
  130.     return -1;
  131.   return atoi (ptr);
  132. }
  133.  
  134. int
  135. tgetflag (cap)
  136.      CONST char *cap;
  137. {
  138.   CONST char *ptr = find_capability (term_entry, cap);
  139.   return 0 != ptr && ptr[-1] == ':';
  140. }
  141.  
  142. /* Look up a string-valued capability CAP.
  143.    If AREA is nonzero, it points to a pointer to a block in which
  144.    to store the string.  That pointer is advanced over the space used.
  145.    If AREA is zero, space is allocated with `malloc'.  */
  146.  
  147. CONST char *
  148. tgetstr (cap, area)
  149.      CONST char *cap;
  150.      char **area;
  151. {
  152.   CONST char *ptr = find_capability (term_entry, cap);
  153.   if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
  154.     return 0;
  155.   return tgetst1 (ptr, area);
  156. }
  157.  
  158. /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
  159.    gives meaning of character following \, or a space if no special meaning.
  160.    Eight characters per line within the string.  */
  161.  
  162. static char esctab[]
  163.   = " \007\010  \033\014 \
  164.       \012 \
  165.   \015 \011 \013 \
  166.         ";
  167.  
  168. /* PTR points to a string value inside a termcap entry.
  169.    Copy that value, processing \ and ^ abbreviations,
  170.    into the block that *AREA points to,
  171.    or to newly allocated storage if AREA is 0.  */
  172.  
  173. static CONST char *
  174. tgetst1 (ptr, area)
  175.      CONST char *ptr;
  176.      char **area;
  177. {
  178.   CONST char *p;
  179.   char *r;
  180.   int c;
  181.   int size;
  182.   char *ret;
  183.   int c1;
  184.  
  185.   if (!ptr)
  186.     return 0;
  187.  
  188.   /* `ret' gets address of where to store the string.  */
  189.   if (!area)
  190.     {
  191.       /* Compute size of block needed (may overestimate).  */
  192.       p = ptr;
  193.       while ((c = *p++) && c != ':' && c != '\n')
  194.     ;
  195.       ret = (char *) xmalloc (p - ptr + 1);
  196.     }
  197.   else
  198.     ret = *area;
  199.  
  200.   /* Copy the string value, stopping at null or colon.
  201.      Also process ^ and \ abbreviations.  */
  202.   p = ptr;
  203.   r = ret;
  204.   while ((c = *p++) && c != ':' && c != '\n')
  205.     {
  206.       if (c == '^')
  207.     c = *p++ & 037;
  208.       else if (c == '\\')
  209.     {
  210.       c = *p++;
  211.       if (c >= '0' && c <= '7')
  212.         {
  213.           c -= '0';
  214.           size = 0;
  215.  
  216.           while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7')
  217.         {
  218.           c *= 8;
  219.           c += c1 - '0';
  220.           p++;
  221.         }
  222.         }
  223.       else if (c >= 0100 && c < 0200)
  224.         {
  225.           c1 = esctab[(c & ~040) - 0100];
  226.           if (c1 != ' ')
  227.         c = c1;
  228.         }
  229.     }
  230.       *r++ = c;
  231.     }
  232.   *r = 0;
  233.   /* Update *AREA.  */
  234.   if (area)
  235.     *area = r + 1;
  236.   return ret;
  237. }
  238.  
  239. /* Outputting a string with padding.  */
  240.  
  241. short ospeed;
  242. /* If OSPEED is 0, we use this as the actual baud rate.  */
  243. int tputs_baud_rate;
  244. char PC;
  245.  
  246. /* Actual baud rate if positive;
  247.    - baud rate / 100 if negative.  */
  248.  
  249. static short speeds[] =
  250.   {
  251. #ifdef VMS
  252.     0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
  253.     -20, -24, -36, -48, -72, -96, -192
  254. #else /* not VMS */
  255.     0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
  256.     -18, -24, -48, -96, -192, -288, -384, -576, -1152
  257. #endif /* not VMS */
  258.   };
  259.  
  260. void
  261. tputs (string, nlines, outfun)
  262.      CONST char *string;
  263.      int nlines;
  264.      void (*outfun) (int);
  265. {
  266.   int padcount = 0;
  267.   int speed;
  268.  
  269. #ifdef emacs
  270.   speed = DEVICE_BAUD_RATE (XDEVICE (Fselected_device ()));
  271. #else
  272.   if (ospeed == 0)
  273.     speed = tputs_baud_rate;
  274.   else
  275.     speed = speeds[ospeed];
  276. #endif
  277.  
  278.   if (string == (char *) 0)
  279.     return;
  280.  
  281.   while (isdigit (* (unsigned char *) string))
  282.     {
  283.       padcount += *string++ - '0';
  284.       padcount *= 10;
  285.     }
  286.   if (*string == '.')
  287.     {
  288.       string++;
  289.       padcount += *string++ - '0';
  290.     }
  291.   if (*string == '*')
  292.     {
  293.       string++;
  294.       padcount *= nlines;
  295.     }
  296.   while (*string)
  297.     (*outfun) (*string++);
  298.  
  299.   /* padcount is now in units of tenths of msec.  */
  300.   padcount *= speeds[ospeed];
  301.   padcount += 500;
  302.   padcount /= 1000;
  303.   if (speeds[ospeed] < 0)
  304.     padcount = -padcount;
  305.   else
  306.     {
  307.       padcount += 50;
  308.       padcount /= 100;
  309.     }
  310.  
  311.   while (padcount-- > 0)
  312.     (*outfun) (PC);
  313. }
  314.  
  315. /* Finding the termcap entry in the termcap data base.  */
  316.  
  317. struct buffer
  318.   {
  319.     char *beg;
  320.     int size;
  321.     char *ptr;
  322.     int ateof;
  323.     int full;
  324.   };
  325.  
  326. /* Forward declarations of static functions.  */
  327.  
  328. static int scan_file ();
  329. static char *gobble_line ();
  330. static int compare_contin ();
  331. static int name_match ();
  332.  
  333. #ifdef VMS
  334.  
  335. #include <rmsdef.h>
  336. #include <fab.h>
  337. #include <nam.h>
  338.  
  339. static int
  340. legal_filename_p (fn)
  341.      char *fn;
  342. {
  343.   struct FAB fab = cc$rms_fab;
  344.   struct NAM nam = cc$rms_nam;
  345.   char esa[NAM$C_MAXRSS];
  346.  
  347.   fab.fab$l_fna = fn;
  348.   fab.fab$b_fns = strlen(fn);
  349.   fab.fab$l_nam = &nam;
  350.   fab.fab$l_fop = FAB$M_NAM;
  351.  
  352.   nam.nam$l_esa = esa;
  353.   nam.nam$b_ess = sizeof esa;
  354.  
  355.   return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL;
  356. }
  357.  
  358. #endif /* VMS */
  359.  
  360. /* Find the termcap entry data for terminal type NAME
  361.    and store it in the block that BP points to.
  362.    Record its address for future use.
  363.  
  364.    If BP is zero, space is dynamically allocated.  */
  365.  
  366. extern char *getenv ();
  367.  
  368. int
  369. tgetent (bp, name)
  370.      char *bp;
  371.      CONST char *name;
  372. {
  373.   char *tem;
  374.   int fd;
  375.   struct buffer buf;
  376.   char *bp1;
  377.   char *bp2;
  378.   CONST char *term;
  379.   int malloc_size = 0;
  380.   int c;
  381.   char *tcenv;            /* TERMCAP value, if it contais :tc=.  */
  382.   CONST char *indirect = 0;    /* Terminal type in :tc= in TERMCAP value.  */
  383.   int filep;
  384.  
  385.   tem = getenv ("TERMCAP");
  386.   if (tem && *tem == 0) tem = 0;
  387.  
  388. #ifdef VMS
  389.   filep = tem && legal_filename_p (tem);
  390. #else
  391.   filep = tem && (*tem == '/');
  392. #endif /* VMS */
  393.  
  394.   /* If tem is non-null and starts with / (in the un*x case, that is),
  395.      it is a file name to use instead of /etc/termcap.
  396.      If it is non-null and does not start with /,
  397.      it is the entry itself, but only if
  398.      the name the caller requested matches the TERM variable.  */
  399.  
  400.   if (tem && !filep && !strcmp (name, (char *) getenv ("TERM")))
  401.     {
  402.       indirect = tgetst1 (find_capability (tem, "tc"), 0);
  403.       if (!indirect)
  404.     {
  405.       if (!bp)
  406.         bp = tem;
  407.       else
  408.         strcpy (bp, tem);
  409.       goto ret;
  410.     }
  411.       else
  412.     {            /* We will need to read /etc/termcap.  */
  413.       tcenv = tem;
  414.        tem = 0;
  415.     }
  416.     }
  417.   else
  418.     indirect = (char *) 0;
  419.  
  420.   if (!tem)
  421. #ifdef VMS
  422.     tem = "emacs_library:[etc]termcap.dat";
  423. #else
  424.     tem = "/etc/termcap";
  425. #endif
  426.  
  427.   /* Here we know we must search a file and tem has its name.  */
  428.  
  429.   fd = open (tem, 0, 0);
  430.   if (fd < 0)
  431.     return -1;
  432.  
  433.   buf.size = BUFSIZE;
  434.   /* Add 1 to size to ensure room for terminating null.  */
  435.   buf.beg = (char *) xmalloc (buf.size + 1);
  436.   term = indirect ? indirect : name;
  437.  
  438.   if (!bp)
  439.     {
  440.       malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
  441.       bp = (char *) xmalloc (malloc_size);
  442.     }
  443.   bp1 = bp;
  444.  
  445.   if (indirect)
  446.     /* Copy the data from the environment variable.  */
  447.     {
  448.       strcpy (bp, tcenv);
  449.       bp1 += strlen (tcenv);
  450.     }
  451.  
  452.   while (term)
  453.     {
  454.       /* Scan the file, reading it via buf, till find start of main entry.  */
  455.       if (scan_file (term, fd, &buf) == 0)
  456.     return 0;
  457.  
  458.       /* Free old `term' if appropriate.  */
  459.       if (term != name)
  460.     xfree (term);
  461.  
  462.       /* If BP is malloc'd by us, make sure it is big enough.  */
  463.       if (malloc_size)
  464.     {
  465.       malloc_size = bp1 - bp + buf.size;
  466.       tem = (char *) xrealloc (bp, malloc_size);
  467.       bp1 += tem - bp;
  468.       bp = tem;
  469.     }
  470.  
  471.       bp2 = bp1;
  472.  
  473.       /* Copy the line of the entry from buf into bp.  */
  474.       tem = buf.ptr;
  475.       while ((*bp1++ = c = *tem++) && c != '\n')
  476.     /* Drop out any \ newline sequence.  */
  477.     if (c == '\\' && *tem == '\n')
  478.       {
  479.         bp1--;
  480.         tem++;
  481.       }
  482.       *bp1 = 0;
  483.  
  484.       /* Does this entry refer to another terminal type's entry?
  485.      If something is found, copy it into heap and null-terminate it.  */
  486.       term = tgetst1 (find_capability (bp2, "tc"), 0);
  487.     }
  488.  
  489.   close (fd);
  490.   xfree (buf.beg);
  491.  
  492.   if (malloc_size)
  493.     {
  494.       bp = (char *) xrealloc (bp, bp1 - bp + 1);
  495.     }
  496.  
  497.  ret:
  498.   term_entry = bp;
  499.   if (malloc_size)
  500.     return (int) bp;
  501.   return 1;
  502. }
  503.  
  504. /* Given file open on FD and buffer BUFP,
  505.    scan the file from the beginning until a line is found
  506.    that starts the entry for terminal type STRING.
  507.    Returns 1 if successful, with that line in BUFP,
  508.    or returns 0 if no entry found in the file.  */
  509.  
  510. static int
  511. scan_file (string, fd, bufp)
  512.      char *string;
  513.      int fd;
  514.      struct buffer *bufp;
  515. {
  516.   char *end;
  517.  
  518.   bufp->ptr = bufp->beg;
  519.   bufp->full = 0;
  520.   bufp->ateof = 0;
  521.   *bufp->ptr = 0;
  522.  
  523.   lseek (fd, 0L, 0);
  524.  
  525.   while (!bufp->ateof)
  526.     {
  527.       /* Read a line into the buffer.  */
  528.       end = 0;
  529.       do
  530.     {
  531.       /* if it is continued, append another line to it,
  532.          until a non-continued line ends.  */
  533.       end = gobble_line (fd, bufp, end);
  534.     }
  535.       while (!bufp->ateof && end[-2] == '\\');
  536.  
  537.       if (*bufp->ptr != '#'
  538.       && name_match (bufp->ptr, string))
  539.     return 1;
  540.  
  541.       /* Discard the line just processed.  */
  542.       bufp->ptr = end;
  543.     }
  544.   return 0;
  545. }
  546.  
  547. /* Return nonzero if NAME is one of the names specified
  548.    by termcap entry LINE.  */
  549.  
  550. static int
  551. name_match (line, name)
  552.      char *line, *name;
  553. {
  554.   char *tem;
  555.  
  556.   if (!compare_contin (line, name))
  557.     return 1;
  558.   /* This line starts an entry.  Is it the right one?  */
  559.   for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
  560.     if (*tem == '|' && !compare_contin (tem + 1, name))
  561.       return 1;
  562.  
  563.   return 0;
  564. }
  565.  
  566. static int
  567. compare_contin (str1, str2)
  568.      char *str1, *str2;
  569. {
  570.   int c1, c2;
  571.   while (1)
  572.     {
  573.       c1 = *str1++;
  574.       c2 = *str2++;
  575.       while (c1 == '\\' && *str1 == '\n')
  576.     {
  577.       str1++;
  578.       while ((c1 = *str1++) == ' ' || c1 == '\t');
  579.     }
  580.       if (c2 == '\0')
  581.     {
  582.       /* End of type being looked up.  */
  583.       if (c1 == '|' || c1 == ':')
  584.         /* If end of name in data base, we win.  */
  585.         return 0;
  586.       else
  587.         return 1;
  588.         }
  589.       else if (c1 != c2)
  590.     return 1;
  591.     }
  592. }
  593.  
  594. /* Make sure that the buffer <- BUFP contains a full line
  595.    of the file open on FD, starting at the place BUFP->ptr
  596.    points to.  Can read more of the file, discard stuff before
  597.    BUFP->ptr, or make the buffer bigger.
  598.  
  599.    Returns the pointer to after the newline ending the line,
  600.    or to the end of the file, if there is no newline to end it.
  601.  
  602.    Can also merge on continuation lines.  If APPEND_END is
  603.    nonzero, it points past the newline of a line that is
  604.    continued; we add another line onto it and regard the whole
  605.    thing as one line.  The caller decides when a line is continued.  */
  606.  
  607. static char *
  608. gobble_line (fd, bufp, append_end)
  609.      int fd;
  610.      struct buffer *bufp;
  611.      char *append_end;
  612. {
  613.   char *end;
  614.   int nread;
  615.   char *buf = bufp->beg;
  616.   char *tem;
  617.  
  618.   if (append_end == 0)
  619.     append_end = bufp->ptr;
  620.  
  621.   while (1)
  622.     {
  623.       end = append_end;
  624.       while (*end && *end != '\n') end++;
  625.       if (*end)
  626.         break;
  627.       if (bufp->ateof)
  628.     return buf + bufp->full;
  629.       if (bufp->ptr == buf)
  630.     {
  631.       if (bufp->full == bufp->size)
  632.         {
  633.           bufp->size *= 2;
  634.           /* Add 1 to size to ensure room for terminating null.  */
  635.           tem = (char *) xrealloc (buf, bufp->size + 1);
  636.           bufp->ptr = (bufp->ptr - buf) + tem;
  637.           append_end = (append_end - buf) + tem;
  638.           bufp->beg = buf = tem;
  639.         }
  640.     }
  641.       else
  642.     {
  643.       append_end -= bufp->ptr - buf;
  644.       memcpy (buf, bufp->ptr, bufp->full -= bufp->ptr - buf);
  645.       bufp->ptr = buf;
  646.     }
  647.       if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full)))
  648.     bufp->ateof = 1;
  649.       bufp->full += nread;
  650.       buf[bufp->full] = 0;
  651.     }
  652.   return end + 1;
  653. }
  654.  
  655. #ifdef TEST
  656.  
  657. #include <stdio.h>
  658.  
  659. main (argc, argv)
  660.      int argc;
  661.      char **argv;
  662. {
  663.   char *term;
  664.   char *buf;
  665.  
  666.   term = argv[1];
  667.   printf ("TERM: %s\n", term);
  668.  
  669.   buf = (char *) tgetent (0, term);
  670.   if ((int) buf <= 0)
  671.     {
  672.       printf ("No entry.\n");
  673.       return 0;
  674.     }
  675.  
  676.   printf ("Entry: %s\n", buf);
  677.  
  678.   tprint ("cm");
  679.   tprint ("AL");
  680.  
  681.   printf ("co: %d\n", tgetnum ("co"));
  682.   printf ("am: %d\n", tgetflag ("am"));
  683. }
  684.  
  685. tprint (cap)
  686.      CONST char *cap;
  687. {
  688.   char *x = tgetstr (cap, 0);
  689.   char *y;
  690.  
  691.   printf ("%s: ", cap);
  692.   if (x)
  693.     {
  694.       for (y = x; *y; y++)
  695.     if (*y <= ' ' || *y == 0177)
  696.       printf ("\\%0o", *y);
  697.     else
  698.       putchar (*y);
  699.       xfree (x);
  700.     }
  701.   else
  702.     printf ("none");
  703.   putchar ('\n');
  704. }
  705.  
  706. #endif /* TEST */
  707.  
  708.